package com.sunxiansheng.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Description: 元数据拦截器
 * 注意事项：在插入和更新时，实体类必须具有fieldName这些字段，并且要有getter和setter方法
 * @Author sun
 * @Create 2024/7/10 14:25
 * @Version 1.0
 */
@Component // 注入容器
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 插入时默认填充的数据
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createBy", String.class, "sunxiansheng");
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "deleteFlag", Integer.class, 0);
        this.strictInsertFill(metaObject, "version", Integer.class, 0);
    }

    /**
     * 更新时默认填充的数据
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateBy", String.class, "sunxiansheng");
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
    }

}